package com.mggz.common.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.type.CollectionType;

import java.io.IOException;
import java.util.List;

/**
 * @Author: Thaddeus
 * @Date: 2023/7/15 9:35
 * @Description: 集合类型的对象和 json 之间的转换
 */
public class ListTransformUtil<T> {
    
    public List<T> transform(String json, Class T) {
        try {
            List<T> tList = null;
            JsonNode data = MapperUtil.MP.readTree(json);
            JsonParser parser = data.traverse();
            CollectionType type = MapperUtil.MP.getTypeFactory().constructCollectionType(List.class, T);
            tList = MapperUtil.MP.readValue(parser, type);
            return tList;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    
}
